package com.cch.demo.sys.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.cch.demo.sys.domain.Loginfo;
import com.cch.demo.sys.service.LoginfoService;
import com.cch.demo.sys.util.ActiverUser;
import com.cch.demo.sys.util.Constast;
import com.cch.demo.sys.util.ResultObj;
import com.cch.demo.sys.util.WebUtils;
import com.cch.demo.sys.vo.UserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("login")
public class LoginContoller {

    @Resource
    private LoginfoService loginfoServiceImpl;


    @RequestMapping("login")
    @ResponseBody
    public ResultObj LoginUser(UserVo userVo){
        String loginname = userVo.getLoginname();
        String pwd = userVo.getPwd();
        String oldcode = WebUtils.getSession().getAttribute("code").toString();
        if(oldcode.equals(userVo.getCode())){
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(loginname,pwd);
            try{
                subject.login(token);
                ActiverUser activerUser = (ActiverUser)subject.getPrincipal();

                //登录日志
                Loginfo entity = new Loginfo();
                entity.setLoginname(activerUser.getUser().getName()+"-"+activerUser.getUser().getLoginname());
                entity.setLoginip(WebUtils.getRequest().getRemoteAddr());
                entity.setLogintime(new Date());

                loginfoServiceImpl.save(entity);
                WebUtils.getSession().setAttribute("user",activerUser.getUser());
                return ResultObj.LOGIN_SUCCESS;
            }catch ( AuthenticationException e){
                e.printStackTrace();
                return ResultObj.LOGIN_ERROR;
            }
        }else{
            return ResultObj.CODE_ERROR;
        }

    }

    /**
     * 验证码生成
     * @throws IOException
     */
    @RequestMapping("getCode")
    public void code(HttpServletResponse resp, HttpSession session) throws IOException {
        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 20);
        String code = captcha.getCode();
        session.setAttribute("code", code);
        ServletOutputStream os = resp.getOutputStream();
        captcha.write(os);
        os.close();

        //第二种方法
        //ImageIO.write(captcha.getImage(), "JPEG", os);
    }
}
